package marble.model {
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class PlanRegButton extends PlanAddRemButton {

		private var userElements:Array;
		private var app:Object = Application.application;

		private var limitExceeded:String;

		public function PlanRegButton(elem:Element, userElements:Array):void {
			this.userElements = userElements;
			super(elem);
			limitExceeded = facade.locale.translate("Capacity limit exceeded");
		}
		
		override protected function onMouseClick(event:MouseEvent):void {
			if (!block) {
				block = true;
				var fun:String = isActive() ? "deleteUserPlanElement" : "addUserPlanElement";
				facade.callRemote(fun, [facade.user_id, elem.id],
					function (event:ResultEvent):void {
						if (event.result == true) {
							app.participantView.loadUserElements(true);
						} else {
							facade.callRemote("canUsePlanElement", [elem.id],
								function (event:ResultEvent):void {
									if (event.result == true) {
										Alert.show(error + "!", "Alert");
									} else {
										Alert.show(limitExceeded + "!", "Alert");
									}
								}
							);
						}
						block = false;
						hideToolTip();
					},
					function (event:FaultEvent):void {
						block = false;
						hideToolTip();
					}
				);
			}
		}

		override protected function isActive():Boolean {
			if (userElements) {
				for (var i:int = 0; i < userElements.length; i++) {
					if (userElements[i].id == elem.id) return true;
				}
			}
			return false;
		}
	}
}